//
//  ServcieChecklistTableView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/19.
//

import UIKit
import RxSwift
import RxCocoa
import RxRelay
import RxDataSources

class ServcieChecklistTableView: BaseChecklistTableView {

    //MARK: - 绑定service checklist 数据源
    override func bindData() {
        self.register(ChecklistHeaderWithSectionImageCell.self, forCellReuseIdentifier: ChecklistHeaderWithSectionImageCell.reuseCellIdentifier)
        self.register(ServiceDetailChecklistTableViewCell.self, forCellReuseIdentifier: ServiceDetailChecklistTableViewCell.reuseCellIdentifier)
        
        self.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        //绑定数据源，自动刷新数据
        ServiceDetailModelHelper.shared.checklists.bind(to: self.rx.items){(tableView, row, checkModel)-> UITableViewCell in
            if checkModel.type == ChecklistType.headerWithImage.rawValue{//header cell with image
                let cell = ChecklistHeaderWithSectionImageCell(style: .default, reuseIdentifier: ChecklistHeaderWithSectionImageCell.reuseCellIdentifier)
                cell.setData(checkModel.title, section: checkModel.section)
                return cell
            }else  {
                //是不是最后一行，默认是
                var lastRowOfSectionFlag: Bool = false
                if row == (ServiceDetailModelHelper.shared.serviceDetailModel.checklists.count - 1){
                    lastRowOfSectionFlag = true
                }
                let cell = ServiceDetailChecklistTableViewCell(style: .default, reuseIdentifier: ServiceDetailChecklistTableViewCell.reuseCellIdentifier)
                cell.setData(checkModel, lastRowFlag: lastRowOfSectionFlag)
                return cell
            }
        }.disposed(by: self.disposeBag)
    }
    override func operateChecklist(_ row: Int) {
        if ServiceDetailModelHelper.shared.serviceDetailModel.status == ServiceJobStatusName.scheduled.rawValue {
            Tool().showAlertViewWithOneBtn(message: "You must set off job before operating checklist.")
            return
        }
        let serverModel = ServiceDetailModelHelper.shared.serviceDetailModel.checklists[row]
        let type = serverModel.type
        if type == ChecklistType.Bool.rawValue {
            self.toClickChecklitBoolItemClosure?(row)
        }else if type == ChecklistType.Image.rawValue{//image,noc，coc，other attachment
            self.toImageListClosure?(row)
        }else if type == ChecklistType.Text.rawValue  {
            self.toEditTextClosure?(serverModel)
        }
    }
}
